package com.ylbh.modules.accessory;

import com.ylbh.common.utils.HttpUtils;
import com.ylbh.common.utils.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author: czx
 * @date: 2019/12/17 13:59
 */
public class YYTApi {

    public static String appKey = "yd8f7d0b54efeaa5f8";
    public static String appSecret = "850ebca5a67a49e19c3dbe23e9631347";
    public static String baseUrl = "http://pre-openapi.380star.com/";
    public static void main(String[] args) {
        System.out.println("同步商品获取商品SpuId列表：" + getSpuIdList());
        System.out.println("根据SPUID 查询SPU详情：" + getSpuDetail());
        System.out.println("根据SpuId  查询SKU列表明细：" + getSkuListDetailBySpuId());
//        System.out.println("查询品牌列表：" + findBrandList());
//        System.out.println("查询分类信息：" + findProdCategory());
//        System.out.println("查询地址信息：" + getAddressInfo());
        System.out.println("查询商品库存：" + findSkuInventory());
        System.out.println("查询商品价格：" + findSkuSalePrice());
//        System.out.println("预占商品库存：" + preHoldSkuInventory());
//        System.out.println("释放预占商品库存：" + releaseSkuInventory());
    }
    /**
     * 同步商品获取商品SpuId列表
     * @return
     */
    private static String getSpuIdList(){
        String method = "/goods/getSpuIdList";
        Map<String, String> params = new HashMap<>();
        params.put("pageIndex", "1");
        params.put("pageSize", "3");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 根据SPUID 查询SPU详情
     * @return
     */
    private static String getSpuDetail(){
        String method = "/goods/getSpuDetail";
        Map<String, String> params = new HashMap<>();
        params.put("spuIds", "169614");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 根据SpuId  查询SKU列表明细
     * @return
     */
    private static String getSkuListDetailBySpuId(){
        String method = "/goods/getSkuListDetailBySpuId";
        Map<String, String> params = new HashMap<>();
        params.put("spuId", "169614");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 查询品牌列表
     * @param pageIndex 当前页 默认第一页
     * @param pageSize  每页大小，默认 100 最大500
     * @return
     */
    private static String findBrandList(){
        String method = "/goods/findBrandList";
        Map<String, String> params = new HashMap<>();
        params.put("pageIndex", "1");
        params.put("pageSize", "2");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 查询分类信息
     * @return
     */
    private static String findProdCategory(){
        String method = "/goods/findProdCategory";
        Map<String, String> params = new HashMap<>();
        params.put("categoryId", "3946");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 查询商品库存
     * @param codes 必填, 商品ID，多个以英文逗号分隔
     * @return
     */
    private static String findSkuInventory(){
        String method = "/goods/findSkuInventory";
        Map<String, String> params = new HashMap<>();
//        params.put("areaId", "4524130,4524157,4524163");
//        params.put("skuIds", "5769");
        params.put("codes", "SL-ECP-120613");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 查询商品价格
     * @param 必填,codes,  最多查200个，多个以英文逗号拼接
     * @return
     */
    private static String findSkuSalePrice(){
        String method = "/goods/findSkuSalePrice";
        Map<String, String> params = new HashMap<>();
//        params.put("skuIds", "5940");
        params.put("codes", "SL-ECP-37234");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 查询地址信息
     * @return
     */
    private static String getAddressInfo(){
        String method = "/address/getAddressInfo";
        Map<String, String> params = new HashMap<>();
        params.put("provinceName", "重庆市");
        params.put("cityName", "重庆市");
        params.put("regionName", "涪陵区");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 预占商品库存
     * @param codeInvList   必填
     * @return
     */
    private static String preHoldSkuInventory(){
        String method = "/goods/preHoldSkuInventory";
        Map<String, String> params = new HashMap<>();
        params.put("areaId", "4524130,4524157,4524163");
        params.put("outOrderNo", "201902268");
//        params.put("skuInvList", "[{\"spuId\":\"59587\",\"skuId\":\"6072\",\"count\":\"10000\"}]");
        params.put("codeInvList", "[{\"code\":\"SL-ECP-6072\",\"quantity\":\"3\"}]");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 释放预占商品库存
     * @param codeInvList   必填
     * @return
     */
    private static String releaseSkuInventory(){
        String method = "/goods/releaseSkuInventory";
        Map<String, String> params = new HashMap<>();
        params.put("outOrderNo", "1234");
//        params.put("skuInvList", "[{\"spuId\":\"115332\",\"skuId\":\"85775\"}]");
        params.put("codeInvList", "[{\"code\":\"SL-ECP-6072\"}]");
        return HttpUtils.sendPost(baseUrl + method, setBaseParams(params));
    }
    /**
     * 设置接口基础参数
     * @param params
     * @return
     */
    private static Map<String, String> setBaseParams(Map<String, String> params){
        params.put("appKey", appKey);
        params.put("currentTime", System.currentTimeMillis() + "");
        String sign = getSign(params, appSecret);
        params.put("sign", sign);
        return params;
    }
    /**
     * 获取签名
     * @param params
     * @param appSecret
     * @return
     */
    public static String getSign(Map<String, String> params, String appSecret) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (!entry.getKey().equals("sign")) {
                // 拼接参数值字符串并进行utf-8解码，防止中文乱码产生
                String value = "";
                try {
                    value = URLDecoder.decode(entry.getValue(), "UTF-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (StringUtils.isEmpty(value)) {
                    continue;
                }
                params.put(entry.getKey(), value);
            }
        }
        // 将参数以参数名的字典升序排序
        Map<String, String> sortParams = new TreeMap<>(params);
        Set<Map.Entry<String, String>> entrys = sortParams.entrySet();
        // 遍历排序的字典,并拼接格式
        StringBuilder valueSb = new StringBuilder();
        for (Map.Entry<String, String> entry : entrys) {
            if (org.apache.commons.lang.StringUtils.isNotBlank(entry.getValue())) {
                valueSb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        valueSb.append(appSecret);
        String sign = valueSb.toString();
        try {
            sign = DigestUtils.md5Hex(sign.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return sign;
    }
}
